<script setup lang="ts">
import { ref } from 'vue'
import { RouterLink } from 'vue-router'

// 组件属性定义和默认值
withDefaults(defineProps<{
  // 可选的标题属性
  title?: string
  // 导航链接数据
  navigationLinks?: Array<{
    path: string
    label: string
    icon?: string
  }>
  // 用户信息
  user?: {
    name: string
    avatar?: string
  }
}>(), {
  title: 'Vue Application',
  navigationLinks: () => [
    { path: '/', label: 'Home' },
    { path: '/about', label: 'About' }
  ],
  user: () => ({
    name: 'Guest User'
  })
})

// 用户菜单状态管理
const isUserMenuOpen = ref(false)

// 切换用户菜单显示状态
const toggleUserMenu = () => {
  isUserMenuOpen.value = !isUserMenuOpen.value
}

// 关闭用户菜单
const closeUserMenu = () => {
  isUserMenuOpen.value = false
}
</script>

<template>
  <header class="header-navigation">
    <!-- 左侧：Logo + 标题 -->
    <div class="header-left">
      <img alt="Vue logo" class="logo" src="@/assets/logo.svg" width="40" height="40" />
      <h1 class="app-title">{{ title }}</h1>
    </div>

    <!-- 中间：导航链接 -->
    <nav class="main-nav">
      <RouterLink 
        v-for="link in navigationLinks" 
        :key="link.path" 
        :to="link.path"
        class="nav-link"
        active-class="nav-link-active"
        exact
      >
        {{ link.label }}
      </RouterLink>
    </nav>

    <!-- 右侧：用户菜单 -->
    <div class="header-right">
      <button 
        class="user-menu-button" 
        @click="toggleUserMenu"
        @blur="closeUserMenu"
        aria-label="User menu"
      >
        <div v-if="user?.avatar" class="user-avatar">
          <img :src="user.avatar" :alt="user.name" class="avatar-img" />
        </div>
        <div v-else class="user-avatar-placeholder">
          {{ user?.name.charAt(0) }}
        </div>
        <span class="user-name">{{ user?.name }}</span>
        <span class="menu-arrow" :class="{ 'arrow-up': isUserMenuOpen }">{{ isUserMenuOpen ? '▲' : '▼' }}</span>
      </button>

      <!-- 用户菜单下拉内容 -->
      <div v-if="isUserMenuOpen" class="user-menu-dropdown">
        <ul class="user-menu-list">
          <li class="user-menu-item">
            <a href="#profile">Profile</a>
          </li>
          <li class="user-menu-item">
            <a href="#settings">Settings</a>
          </li>
          <li class="user-menu-divider"></li>
          <li class="user-menu-item">
            <a href="#logout">Logout</a>
          </li>
        </ul>
      </div>
    </div>
  </header>
</template>

<style scoped>
.header-navigation {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1.5rem;
  background-color: var(--color-background);
  border-bottom: 1px solid var(--color-border);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  height: 64px;
  position: sticky;
  top: 0;
  z-index: 100;
}

/* 左侧 Logo 和标题 */
.header-left {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.logo {
  height: 40px;
  width: auto;
  display: block;
}

.app-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--color-text);
  margin: 0;
}

/* 中间导航 */
.main-nav {
  display: flex;
  gap: 1.5rem;
  margin: 0 auto;
}

.nav-link {
  color: var(--color-text);
  text-decoration: none;
  padding: 0.5rem 0.75rem;
  border-radius: 4px;
  font-size: 0.95rem;
  transition: all 0.3s ease;
  position: relative;
}

.nav-link:hover {
  background-color: rgba(0, 0, 0, 0.05);
  color: var(--color-text);
}

.nav-link-active {
  font-weight: 600;
  color: hsla(160, 100%, 37%, 1);
}

.nav-link-active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 20px;
  height: 2px;
  background-color: hsla(160, 100%, 37%, 1);
}

/* 右侧用户菜单 */
.header-right {
  position: relative;
}

.user-menu-button {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  background: transparent;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  cursor: pointer;
  color: var(--color-text);
  font-size: 0.9rem;
  transition: all 0.3s ease;
  min-width: 120px;
  justify-content: center;
}

.user-menu-button:hover {
  background-color: var(--color-background-soft);
  border-color: var(--color-border-hover);
}

.user-avatar, .user-avatar-placeholder {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: hsla(160, 100%, 37%, 0.2);
}

.avatar-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.user-avatar-placeholder {
  font-weight: 600;
  color: hsla(160, 100%, 37%, 1);
  font-size: 0.85rem;
}

.user-name {
  font-weight: 500;
  flex: 1;
}

.menu-arrow {
  font-size: 0.7rem;
  transition: transform 0.2s ease;
}

/* 用户下拉菜单 */
.user-menu-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 0.5rem;
  background-color: var(--color-background);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  min-width: 180px;
  z-index: 1000;
}

.user-menu-list {
  list-style: none;
  padding: 0.5rem 0;
  margin: 0;
}

.user-menu-item {
  margin: 0;
}

.user-menu-item a {
  display: block;
  padding: 0.5rem 1rem;
  color: var(--color-text);
  text-decoration: none;
  font-size: 0.9rem;
  transition: background-color 0.2s ease;
}

.user-menu-item a:hover {
  background-color: var(--color-background-soft);
}

.user-menu-divider {
  height: 1px;
  background-color: var(--color-border);
  margin: 0.5rem 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-navigation {
    padding: 0.75rem 1rem;
  }
  
  .app-title {
    display: none;
  }
  
  .main-nav {
    gap: 1rem;
  }
  
  .nav-link {
    font-size: 0.85rem;
    padding: 0.4rem 0.6rem;
  }
  
  .user-menu-button {
    min-width: auto;
    padding: 0.4rem 0.6rem;
  }
  
  .user-name {
    display: none;
  }
}

@media (max-width: 480px) {
  .main-nav {
    gap: 0.5rem;
  }
  
  .nav-link {
    font-size: 0.8rem;
    padding: 0.3rem 0.5rem;
  }
}
</style>